﻿using System;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;

using CE.User;
using CE.Global;
using CE.Database;
using CE.CarInsurance;

public partial class carinsurance_QuoteDownload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            if (Request.QueryString["carid"] != null && Request.QueryString["userid"] != null)
            {
                Int64 CECarId = Convert.ToInt64(Request.QueryString["carid"].ToString());
                Int64 UserId = Convert.ToInt64(Request.QueryString["userid"].ToString());
                GenerateExcelQuote(CECarId, UserId);
            }
        }
    }

    protected void GenerateExcelQuote(Int64 CECarId, Int64 UserId)
    {
        Label lblContent = new Label();
        string _fileName = "FileName";

        StringBuilder strText = new StringBuilder("");
        strText.Append("<table><tr><td colspan='4'><h3>Car Insurance : Quote Request</h3></td></tr></table>");

        #region Variable Declaration

        string _html, _name, _gender, _nric, _dob, _dd, _mm, _yy, _occupation, _jobnature, _maritalstatus, _yearsofdriving, _manufactureyear, _registeryear;
        string _carmodel, _offpeek, _ncdpercent, _applicablencd, _offancedriving, _additionaldriver, _commencementdate, _email, _mobile, _vehicleno, _inscomp;
        string _drname1, _drage1, _drgender1, _drmarital1, _drocc1, _drexp1, _drjobnature1, _drname2, _drage2, _drgender2, _drmarital2, _drocc2, _drexp2, _drjobnature2;
        string _drname3, _drage3, _drgender3, _drmarital3, _drocc3, _drexp3, _drjobnature3, _drname4, _drage4, _drgender4, _drmarital4, _drocc4, _drexp4, _drjobnature4;
        string _drname5, _drage5, _drgender5, _drmarital5, _drocc5, _drexp5, _drjobnature5, _drcount, _drDOB1, _drDOB2, _drDOB3, _drDOB4, _drDOB5, _indd, _inmm, _inyy;
        string _claimstatus, _claim1ststat, _claim1stend, _claimcount1, claimamt1, _claim2ndstat, _claim2ndend, claimamt2, _claim3rdstat, _claim3rdend, claimamt3;
        string _insdate, _covertypedesc;
string _suspension, _policycancelled, _driver1relation, _driver2relation, _driver3relation, _driver4relation, _driver5relation;
        string _driver1name, _driver1nric, _driver2name, _driver2nric;
        string _driver3name, _driver3nric, _driver4name, _driver4nric, _driver5name, _driver5nric;

        string _drivinglicensepass, _registerdate, _driver1licensepassdate, _driver2licensepassdate, _driver3licensepassdate, _driver4licensepassdate, _driver5licensepassdate;
        _drivinglicensepass = ""; _registerdate = ""; _driver1licensepassdate = ""; _driver2licensepassdate = ""; _driver3licensepassdate = ""; 
        _driver4licensepassdate = ""; _driver5licensepassdate = "";

        DateTime _ddob, _indate;
        DateTime _drddob1 = DateTime.Now;
        DateTime _drddob2 = DateTime.Now;
        DateTime _drddob3 = DateTime.Now;
        DateTime _drddob4 = DateTime.Now;
        DateTime _drddob5 = DateTime.Now;
        DateTime _dtdrivinglicensepass = DateTime.Now;
        DateTime _dtregisterdate = DateTime.Now;
        DateTime _claimlast3AccidentDate1 = DateTime.Now;
        DateTime _claimlast3AccidentDate2 = DateTime.Now;
        DateTime _claimlast3AccidentDate3 = DateTime.Now;
        DateTime _claimlast2AccidentDate1 = DateTime.Now;
        DateTime _claimlast2AccidentDate2 = DateTime.Now;
        DateTime _claimlast2AccidentDate3 = DateTime.Now;
        DateTime _claimlast1AccidentDate1 = DateTime.Now;
        DateTime _claimlast1AccidentDate2 = DateTime.Now;
        DateTime _claimlast1AccidentDate3 = DateTime.Now;

        DateTime _dtdriver1licensepassdate = DateTime.Now;
        DateTime _dtdriver2licensepassdate = DateTime.Now;
        DateTime _dtdriver3licensepassdate = DateTime.Now;
        DateTime _dtdriver4licensepassdate = DateTime.Now;
        DateTime _dtdriver5licensepassdate = DateTime.Now;

        DateTime _dtclaim1ststat = DateTime.Now; 
        DateTime _dtclaim1stend = DateTime.Now; 
        DateTime _dtclaim2ndstat = DateTime.Now; 
        DateTime _dtclaim2ndend = DateTime.Now;
        DateTime _dtclaim3rdstat = DateTime.Now;
        DateTime _dtclaim3rdend = DateTime.Now;

        _html = ""; _name = ""; _gender = ""; _nric = ""; _dob = ""; _dd = ""; _mm = ""; _yy = ""; _occupation = ""; _jobnature = ""; _maritalstatus = "";
        _yearsofdriving = ""; _manufactureyear = ""; _registeryear = ""; _carmodel = ""; _offpeek = ""; _ncdpercent = ""; _applicablencd = "";
        _offancedriving = ""; _additionaldriver = ""; _commencementdate = ""; _email = ""; _mobile = ""; _vehicleno = ""; _inscomp = "";
        _drname1 = ""; _drage1 = ""; _drgender1 = ""; _drmarital1 = ""; _drocc1 = ""; _drexp1 = ""; _drjobnature1 = "";
        _drname2 = ""; _drage2 = ""; _drgender2 = ""; _drmarital2 = ""; _drocc2 = ""; _drexp2 = ""; _drjobnature2 = "";
        _drname3 = ""; _drage3 = ""; _drgender3 = ""; _drmarital3 = ""; _drocc3 = ""; _drexp3 = ""; _drjobnature3 = "";
        _drname4 = ""; _drage4 = ""; _drgender4 = ""; _drmarital4 = ""; _drocc4 = ""; _drexp4 = ""; _drjobnature4 = "";
        _drname5 = ""; _drage5 = ""; _drgender5 = ""; _drmarital5 = ""; _drocc5 = ""; _drexp5 = ""; _drjobnature5 = ""; _drcount = "0";
        _drDOB1 = ""; _drDOB2 = ""; _drDOB3 = ""; _drDOB4 = ""; _drDOB5 = ""; _indd = ""; _inmm = ""; _inyy = "";
        _claimstatus = ""; _claim1ststat = ""; _claim1stend = ""; _claimcount1 = ""; claimamt1 = ""; _claim2ndstat = ""; _claim2ndend = "";
        claimamt2 = ""; _claim3rdstat = ""; _claim3rdend = ""; claimamt3 = ""; _covertypedesc = ""; 
        _suspension = ""; _policycancelled = ""; _driver1relation = ""; _driver2relation = ""; _driver3relation = ""; _driver4relation = ""; _driver5relation = "";

        _driver1name = ""; _driver1nric = ""; _driver2name = ""; _driver2nric = ""; _driver3name = ""; _driver3nric = "";
        _driver4name = ""; _driver4nric = ""; _driver5name = ""; _driver5nric = "";

        #endregion

        DataSet _dsUserQuotes = new DataSet();
        BrokerPremiumDB _brokerpremiumdb = new BrokerPremiumDB();
        _dsUserQuotes = _brokerpremiumdb.GetCXCICRMQuotes(CECarId, UserId, "CI");
        if (_dsUserQuotes.Tables[0].Rows.Count > 0)
        {
            #region variables
            _name = _dsUserQuotes.Tables[0].Rows[0]["Name"].ToString();
            _nric = _dsUserQuotes.Tables[0].Rows[0]["NRIC"].ToString();
            _manufactureyear = _dsUserQuotes.Tables[0].Rows[0]["ManufactYear"].ToString();
            _registeryear = _dsUserQuotes.Tables[0].Rows[0]["RegYear"].ToString();
            _carmodel = _dsUserQuotes.Tables[0].Rows[0]["MakeModelDesc"].ToString();
            //_ddob = Convert.ToDateTime(_dsUserQuotes.Tables[0].Rows[0]["DOB"].ToString());
            //_dd = _ddob.Day.ToString("00");
            //_mm = _ddob.Month.ToString("00");
            //_yy = _ddob.Year.ToString();
            //_dob = _dd + "/" + _mm + "/" + _yy;
            _dob = _dsUserQuotes.Tables[0].Rows[0]["DOB"].ToString();
            _maritalstatus = _dsUserQuotes.Tables[0].Rows[0]["Marital"].ToString();
            _gender = _dsUserQuotes.Tables[0].Rows[0]["Gender"].ToString();
            _jobnature = _dsUserQuotes.Tables[0].Rows[0]["JobNature"].ToString();
            _email = _dsUserQuotes.Tables[0].Rows[0]["Email"].ToString();
            _yearsofdriving = _dsUserQuotes.Tables[0].Rows[0]["YearDriveSg"].ToString();
            _occupation = _dsUserQuotes.Tables[0].Rows[0]["Description"].ToString();
            _mobile = _dsUserQuotes.Tables[0].Rows[0]["Mobile"].ToString();
            _suspension = _dsUserQuotes.Tables[0].Rows[0]["LicenseSuspension"].ToString();
            _policycancelled = _dsUserQuotes.Tables[0].Rows[0]["PolicyCancelled"].ToString();
            _covertypedesc = _dsUserQuotes.Tables[0].Rows[0]["ProviderCoverTypeDesc"].ToString();


            if (_dsUserQuotes.Tables[0].Rows[0]["IsClaimLastThreeYear"].ToString() == "0")
            {
                _claimstatus = "No";
            }
            else
            {
                _claimstatus = "Yes";
            }
            _ncdpercent = _dsUserQuotes.Tables[0].Rows[0]["NCDPercent"].ToString();
            if (_ncdpercent.ToString() == "-1")
            {
                _ncdpercent = "No NCD(First Policy)";
            }
            else
            {
                _ncdpercent += "%";
            }
            _applicablencd = _dsUserQuotes.Tables[0].Rows[0]["ApplicableNCD"].ToString();
            _additionaldriver = _dsUserQuotes.Tables[0].Rows[0]["AddDriver"].ToString();
            _offpeek = _dsUserQuotes.Tables[0].Rows[0]["Offbeak"].ToString();
            _offancedriving = _dsUserQuotes.Tables[0].Rows[0]["IsOffDriveCom"].ToString();
            _drcount = _dsUserQuotes.Tables[0].Rows[0]["NoOfDriver"].ToString();
            if (_dsUserQuotes.Tables[1].Rows.Count > 0)
            {
                _claimstatus = "Yes";
                _claim1ststat = _dsUserQuotes.Tables[1].Rows[0]["Claim1stStartDate"].ToString();
                _claim1stend = _dsUserQuotes.Tables[1].Rows[0]["Claim1stEndDate"].ToString();

                //_dtclaim1ststat = Convert.ToDateTime(_dsUserQuotes.Tables[1].Rows[0]["Claim1stStartDate"].ToString());
                //_dtclaim1stend = Convert.ToDateTime(_dsUserQuotes.Tables[1].Rows[0]["Claim1stEndDate"].ToString());

                claimamt1 = _dsUserQuotes.Tables[1].Rows[0]["NOCLast1Value"].ToString();
                _claimcount1 = _dsUserQuotes.Tables[1].Rows[0]["NOCLast1"].ToString();

                _claim2ndstat = _dsUserQuotes.Tables[1].Rows[0]["Claim2ndStartDate"].ToString();
                _claim2ndend = _dsUserQuotes.Tables[1].Rows[0]["Claim2ndEndDate"].ToString();

                //_dtclaim2ndstat = Convert.ToDateTime(_dsUserQuotes.Tables[1].Rows[0]["Claim2ndStartDate"].ToString());
                //_dtclaim2ndend = Convert.ToDateTime(_dsUserQuotes.Tables[1].Rows[0]["Claim2ndEndDate"].ToString());

                claimamt2 = _dsUserQuotes.Tables[1].Rows[0]["NOCLast2Value"].ToString();

                _claim3rdstat = _dsUserQuotes.Tables[1].Rows[0]["Claim3rdStartDate"].ToString();
                _claim3rdend = _dsUserQuotes.Tables[1].Rows[0]["Claim3rdEndDate"].ToString();

                //_dtclaim3rdstat = Convert.ToDateTime(_dsUserQuotes.Tables[1].Rows[0]["Claim3rdStartDate"].ToString());
                //_dtclaim3rdend = Convert.ToDateTime(_dsUserQuotes.Tables[1].Rows[0]["Claim3rdEndDate"].ToString());

                claimamt3 = _dsUserQuotes.Tables[1].Rows[0]["NOCLast3Value"].ToString();
            }
            else
            {
                _claim1ststat = "";
                _claim1stend = "";
                claimamt1 = "";
                _claimcount1 = "0";

                _claim2ndstat = "";
                _claim2ndend = "";
                claimamt2 = "";

                _claim3rdstat = "";
                _claim3rdend = "";
                claimamt3 = "";
                _claimstatus = "No";
            }


            _vehicleno = _dsUserQuotes.Tables[0].Rows[0]["VehicleNumber"].ToString();
            //_dtdrivinglicensepass = Convert.ToDateTime(_dsUserQuotes.Tables[0].Rows[0]["LicensePassDate"].ToString());
            //string _lpdd = _dtdrivinglicensepass.Day.ToString("00");
            //string _lpmm = _dtdrivinglicensepass.Month.ToString("00");
            //string _lpyy = _dtdrivinglicensepass.Year.ToString();
            //_drivinglicensepass = _lpdd + "/" + _lpmm + "/" + _lpyy;
            _drivinglicensepass = _dsUserQuotes.Tables[0].Rows[0]["LicensePassDate"].ToString();

            //_dtregisterdate = Convert.ToDateTime(_dsUserQuotes.Tables[0].Rows[0]["RegistrationDate"].ToString());
            //string _rgdd = _dtregisterdate.Day.ToString("00");
            //string _rgmm = _dtregisterdate.Month.ToString("00");
            //string _rgyy = _dtregisterdate.Year.ToString();
            //_registerdate = _rgdd + "/" + _rgmm + "/" + _rgyy;
            _registerdate = _dsUserQuotes.Tables[0].Rows[0]["RegistrationDate"].ToString();

            //_indate = Convert.ToDateTime(_dsUserQuotes.Tables[0].Rows[0]["CoverageCommenceDate"].ToString());
            //_indd = _indate.Day.ToString("00");
            //_inmm = _indate.Month.ToString("00");
            //_inyy = _indate.Year.ToString();
            //_insdate = _indd + "/" + _inmm + "/" + _inyy;
            _insdate = _dsUserQuotes.Tables[0].Rows[0]["CoverageCommenceDate"].ToString();

            if (_dsUserQuotes.Tables[2].Rows.Count > 0)
            {
                _inscomp = _dsUserQuotes.Tables[2].Rows[0]["InsCompanyName"].ToString();
            }
            if (_dsUserQuotes.Tables[3].Rows.Count > 0)
            {
                if (Convert.ToInt16(_drcount) > 0)
                {
                    _drage1 = _dsUserQuotes.Tables[3].Rows[0]["Age"].ToString();
                    _drgender1 = _dsUserQuotes.Tables[3].Rows[0]["Gender"].ToString();
                    _drmarital1 = _dsUserQuotes.Tables[3].Rows[0]["Marital"].ToString();
                    _drocc1 = _dsUserQuotes.Tables[3].Rows[0]["Description"].ToString();
                    _drexp1 = _dsUserQuotes.Tables[3].Rows[0]["YearOfDrivingInSg"].ToString();
                    _drDOB1 = _dsUserQuotes.Tables[3].Rows[0]["DOB"].ToString();
                    _drjobnature1 = _dsUserQuotes.Tables[3].Rows[0]["JobNature"].ToString();
                }
                if (Convert.ToInt16(_drcount) > 1)
                {
                    _drage2 = _dsUserQuotes.Tables[3].Rows[1]["Age"].ToString();
                    _drgender2 = _dsUserQuotes.Tables[3].Rows[1]["Gender"].ToString();
                    _drmarital2 = _dsUserQuotes.Tables[3].Rows[1]["Marital"].ToString();
                    _drocc2 = _dsUserQuotes.Tables[3].Rows[1]["Description"].ToString();
                    _drexp2 = _dsUserQuotes.Tables[3].Rows[1]["YearOfDrivingInSg"].ToString();
                    _drDOB2 = _dsUserQuotes.Tables[3].Rows[1]["DOB"].ToString();
                    _drjobnature2 = _dsUserQuotes.Tables[3].Rows[1]["JobNature"].ToString();
                }
                if (Convert.ToInt16(_drcount) > 2)
                {
                    _drage3 = _dsUserQuotes.Tables[3].Rows[2]["Age"].ToString();
                    _drgender3 = _dsUserQuotes.Tables[3].Rows[2]["Gender"].ToString();
                    _drmarital3 = _dsUserQuotes.Tables[3].Rows[2]["Marital"].ToString();
                    _drocc3 = _dsUserQuotes.Tables[3].Rows[2]["Description"].ToString();
                    _drexp3 = _dsUserQuotes.Tables[3].Rows[2]["YearOfDrivingInSg"].ToString();
                    _drDOB3 = _dsUserQuotes.Tables[3].Rows[2]["DOB"].ToString();
                    _drjobnature3 = _dsUserQuotes.Tables[3].Rows[2]["JobNature"].ToString();
                }
                if (Convert.ToInt16(_drcount) > 3)
                {
                    _drage4 = _dsUserQuotes.Tables[3].Rows[3]["Age"].ToString();
                    _drgender4 = _dsUserQuotes.Tables[3].Rows[3]["Gender"].ToString();
                    _drmarital4 = _dsUserQuotes.Tables[3].Rows[3]["Marital"].ToString();
                    _drocc4 = _dsUserQuotes.Tables[3].Rows[3]["Description"].ToString();
                    _drexp4 = _dsUserQuotes.Tables[3].Rows[3]["YearOfDrivingInSg"].ToString();
                    _drDOB4 = _dsUserQuotes.Tables[3].Rows[3]["DOB"].ToString();
                    _drjobnature4 = _dsUserQuotes.Tables[3].Rows[3]["JobNature"].ToString();
                }
                if (Convert.ToInt16(_drcount) > 4)
                {
                    _drage5 = _dsUserQuotes.Tables[3].Rows[4]["Age"].ToString();
                    _drgender5 = _dsUserQuotes.Tables[3].Rows[4]["Gender"].ToString();
                    _drmarital5 = _dsUserQuotes.Tables[3].Rows[4]["Marital"].ToString();
                    _drocc5 = _dsUserQuotes.Tables[3].Rows[4]["Description"].ToString();
                    _drexp5 = _dsUserQuotes.Tables[3].Rows[4]["YearOfDrivingInSg"].ToString();
                    _drDOB5 = _dsUserQuotes.Tables[3].Rows[4]["DOB"].ToString();
                    _drjobnature5 = _dsUserQuotes.Tables[3].Rows[4]["JobNature"].ToString();
                }
            }
            if (_dsUserQuotes.Tables.Count > 3)
            {
                if (_dsUserQuotes.Tables[4].Rows.Count > 0)
                {
                    _driver1name = _dsUserQuotes.Tables[4].Rows[0]["Name"].ToString();
                    _driver1nric = _dsUserQuotes.Tables[4].Rows[0]["NRIC"].ToString();
                    //_dtdriver1licensepassdate = Convert.ToDateTime(_dsUserQuotes.Tables[4].Rows[0]["LicensePassDate"].ToString());
                    //string _dr1lpdd = _dtdriver1licensepassdate.Day.ToString("00");
                    //string _dr1lpmm = _dtdriver1licensepassdate.Month.ToString("00");
                    //string _dr1lpyy = _dtdriver1licensepassdate.Year.ToString();
                    //_driver1licensepassdate = _dr1lpdd + "/" + _dr1lpmm + "/" + _dr1lpyy;
                    _driver1licensepassdate = _dsUserQuotes.Tables[4].Rows[0]["LicensePassDate"].ToString();
                    _driver1relation = _dsUserQuotes.Tables[4].Rows[0]["Relation"].ToString();
                }
                if (_dsUserQuotes.Tables[4].Rows.Count > 1)
                {
                    _driver2name = _dsUserQuotes.Tables[4].Rows[1]["Name"].ToString();
                    _driver2nric = _dsUserQuotes.Tables[4].Rows[1]["NRIC"].ToString();
                    //_dtdriver2licensepassdate = Convert.ToDateTime(_dsUserQuotes.Tables[4].Rows[1]["LicensePassDate"].ToString());
                    //string _dr2lpdd = _dtdriver2licensepassdate.Day.ToString("00");
                    //string _dr2lpmm = _dtdriver2licensepassdate.Month.ToString("00");
                    //string _dr2lpyy = _dtdriver2licensepassdate.Year.ToString();
                    //_driver2licensepassdate = _dr2lpdd + "/" + _dr2lpmm + "/" + _dr2lpyy;
                    _driver2licensepassdate = _dsUserQuotes.Tables[4].Rows[1]["LicensePassDate"].ToString();
                    _driver2relation = _dsUserQuotes.Tables[4].Rows[1]["Relation"].ToString();
                }
                if (_dsUserQuotes.Tables[4].Rows.Count > 2)
                {
                    _driver3name = _dsUserQuotes.Tables[4].Rows[2]["Name"].ToString();
                    _driver3nric = _dsUserQuotes.Tables[4].Rows[2]["NRIC"].ToString();
                    //_dtdriver3licensepassdate = Convert.ToDateTime(_dsUserQuotes.Tables[4].Rows[2]["LicensePassDate"].ToString());
                    //string _dr3lpdd = _dtdriver3licensepassdate.Day.ToString("00");
                    //string _dr3lpmm = _dtdriver3licensepassdate.Month.ToString("00");
                    //string _dr3lpyy = _dtdriver3licensepassdate.Year.ToString();
                    //_driver3licensepassdate = _dr3lpdd + "/" + _dr3lpmm + "/" + _dr3lpyy;
                    _driver3licensepassdate = _dsUserQuotes.Tables[4].Rows[2]["LicensePassDate"].ToString();
                    _driver3relation = _dsUserQuotes.Tables[4].Rows[2]["Relation"].ToString();
                }
                if (_dsUserQuotes.Tables[4].Rows.Count > 3)
                {
                    _driver4name = _dsUserQuotes.Tables[4].Rows[3]["Name"].ToString();
                    _driver4nric = _dsUserQuotes.Tables[4].Rows[3]["NRIC"].ToString();
                    //_dtdriver4licensepassdate = Convert.ToDateTime(_dsUserQuotes.Tables[4].Rows[3]["LicensePassDate"].ToString());
                    //string _dr4lpdd = _dtdriver4licensepassdate.Day.ToString("00");
                    //string _dr4lpmm = _dtdriver4licensepassdate.Month.ToString("00");
                    //string _dr4lpyy = _dtdriver4licensepassdate.Year.ToString();
                    //_driver4licensepassdate = _dr4lpdd + "/" + _dr4lpmm + "/" + _dr4lpyy;
                    _driver4licensepassdate = _dsUserQuotes.Tables[4].Rows[3]["LicensePassDate"].ToString();
                    _driver4relation = _dsUserQuotes.Tables[4].Rows[3]["Relation"].ToString();
                }
                if (_dsUserQuotes.Tables[4].Rows.Count > 4)
                {
                    _driver5name = _dsUserQuotes.Tables[4].Rows[4]["Name"].ToString();
                    _driver5nric = _dsUserQuotes.Tables[4].Rows[4]["NRIC"].ToString();
                    //_dtdriver5licensepassdate = Convert.ToDateTime(_dsUserQuotes.Tables[4].Rows[4]["LicensePassDate"].ToString());
                    //string _dr5lpdd = _dtdriver5licensepassdate.Day.ToString("00");
                    //string _dr5lpmm = _dtdriver5licensepassdate.Month.ToString("00");
                    //string _dr5lpyy = _dtdriver5licensepassdate.Year.ToString();
                    //_driver5licensepassdate = _dr5lpdd + "/" + _dr5lpmm + "/" + _dr5lpyy;
                    _driver5licensepassdate = _dsUserQuotes.Tables[4].Rows[4]["LicensePassDate"].ToString();
                    _driver5relation = _dsUserQuotes.Tables[4].Rows[4]["Relation"].ToString();
                }
            }

            #endregion
            
            #region Html
            //_html = "<table><tr><td colspan='4'> </td></tr></table>";

            _html += "<table style='width: 100%; background-color: white;' cellpadding='0' cellspacing='0' border='1'>"
                     + "<tr>"
                     + "<td width='50%'>" //Start 1
                     + "<table style='width: 100%; background-color: white;' cellpadding='0' cellspacing='0' border='1'>"

                     + "<tr>"
                     + "<td width='50%' align='left'>Name</td>"
                     + "<td width='50%' align='left'>" + _name.ToString() + "</td>"
                     + "</tr>"

                     + "<tr>"
                     + "<td width='50%' align='left'>Marital Status</td>"
                     + "<td width='50%' align='left'>" + _maritalstatus.ToString() + "</td>"
                     + "</tr>"

                     + "<tr>"
                     + "<td width='50%' align='left'>Occupation / Business</td>"
                     + "<td width='50%' align='left'>" + _occupation.ToString() + "</td>"
                     + "</tr>"

                     + "<tr>"
                     + "<td width='50%' align='left'>Mobile No</td>"
                     + "<td width='50%' align='left'>" + _mobile.ToString() + "</td>"
                     + "</tr>"

                     + "<tr>"
                     + "<td width='50%' align='left'>NRIC</td>"
                     + "<td width='50%' align='left'>" + _nric.ToString() + "</td>"
                     + "</tr>"

                     + "<tr>"
                     + "<td width='50%' align='left'>Car MakeModel</td>"
                     + "<td width='50%' align='left'>" + _carmodel.ToString() + "</td>"
                     + "</tr>"

                     + "<tr>"
                     + "<td width='50%' align='left'>Car Manufacture Year</td>"
                     + "<td width='50%' align='left'>" + _manufactureyear.ToString() + "</td>"
                     + "</tr>"

                     + "<tr>"
                     + "<td width='50%' align='left'>Existing NCD</td>"
                     + "<td width='50%' align='left'>" + _ncdpercent.ToString() + "</td>"
                     + "</tr>"

                     + "<tr>"
                     + "<td width='50%' align='left'>Off Peak</td>"
                     + "<td width='50%' align='left'>" + _offpeek.ToString() + "</td>"
                     + "</tr>"



                     + "</table>"
                     + "</td>" //End 1

                     + "<td width='50%'>" //Start 2
                     + "<table style='width: 100%; background-color: white;' cellpadding='0' cellspacing='0' border='1'>"

                     + "<tr>"
                     + "<td width='50%' align='left'>Date of Birth</td>"
                     + "<td width='50%' align='left'>" + _dob.ToString() + "</td>"
                     // + "<td width='50%' align='left'>" + _ddob.ToString("dd-MMM-yyyy") + "</td>"
                     + "</tr>"

                     + "<tr>"
                     + "<td width='50%' align='left'>Gender</td>"
                     + "<td width='50%' align='left'>" + _gender.ToString() + "</td>"
                     + "</tr>"

                     + "<tr>"
                     + "<td width='50%' align='left'>Email</td>"
                     + "<td width='50%' align='left'>" + _email.ToString() + "</td>"
                     + "</tr>"

                     + "<tr>"
                     + "<td width='50%' align='left'>Driving Experience</td>"
                     + "<td width='50%' align='left'>" + _yearsofdriving.ToString() + "</td>"
                     + "</tr>"

                     + "<tr>"
                     + "<td width='50%' align='left'>Commencement Date</td>"
                     + "<td width='50%' align='left'>" + _insdate.ToString() + "</td>"
                     //+ "<td width='50%' align='left'>" + _indate.ToString("dd-MMM-yyyy") + "</td>"
                     + "</tr>"

                     + "<tr>"
                     + "<td width='50%' align='left'>Vehicle No</td>"
                     + "<td width='50%' align='left'>" + _vehicleno.ToString() + "</td>"
                     + "</tr>"

                     + "<tr>"
                     + "<td width='50%' align='left'>Register Year</td>"
                     + "<td width='50%' align='left'>" + _registeryear.ToString() + "</td>"
                     + "</tr>"

                     + "<tr>"
                     + "<td width='50%' align='left'>NCD on next renewal (%)</td>"
                     + "<td width='50%' align='left'>" + _applicablencd.ToString() + "</td>"
                     + "</tr>"

                     + "<tr>"
                     + "<td width='50%' align='left'>Offence-Free Driving</td>"
                     + "<td width='50%' align='left'>" + _offancedriving.ToString() + "</td>"
                     + "</tr>"

                     + "</table>"
                     + "</td>" //End 2
                     + "</tr>";

            _html += "</table>";

            _html += "<table style='width: 100%; background-color: white;' cellpadding='0' cellspacing='0' border='1'>";
            _html += "<tr><td colspan='4' align='left'><b>Driving License Pass Date: </b>" + _drivinglicensepass.ToString() + "</td></tr>";
            //_html += "<tr><td colspan='4' align='left'><b>Driving License Pass Date: </b>" + _dtdrivinglicensepass.ToString("dd-MMM-yyyy") + "</td></tr>";
            _html += "</table>";

            _html += "<table style='width: 100%; background-color: white;' cellpadding='0' cellspacing='0' border='1'>";
            _html += "<tr><td colspan='4' align='left'><b>Register Date: </b>" + _registerdate.ToString() + "</td></tr>";
            //_html += "<tr><td colspan='4' align='left'><b>Register Date: </b>" + _dtregisterdate.ToString("dd-MMM-yyyy") + "</td></tr>";
            _html += "</table>";

            _html += "<table style='width: 100%; background-color: white;' cellpadding='0' cellspacing='0' border='1'>";
            _html += "<tr><td colspan='4' align='left'><b>Any Suspension of Driving License? </b>" + _suspension + "</td></tr>";
            _html += "</table>";

            _html += "<table style='width: 100%; background-color: white;' cellpadding='0' cellspacing='0' border='1'>";
            _html += "<tr><td colspan='4' align='left'><b> Any motor policy cancelled or declined for renewal by insurers? </b>" + _policycancelled + "</td></tr>";
            _html += "</table>";

            _html += "<table style='width: 100%; background-color: white;' cellpadding='0' cellspacing='0' border='1'>";
            _html += "<tr><td colspan='4' align='left'><b>Current Insurer : </b>" + _inscomp.ToString() + "</td></tr>";
            _html += "</table>";

            _html += "<table style='width: 100%; background-color: white;' cellpadding='0' cellspacing='0' border='1'>";
            _html += "<tr><td colspan='4' align='left'><b>Cover Type : </b>" + _covertypedesc.ToString() + "</td></tr>";
            _html += "</table>";

            //_html += "<table style='width: 100%; background-color: white;' cellpadding='0' cellspacing='0' border='1'>";
            //_html += "<tr><td colspan='4' align='left'><b>Renewal Quote Received : </b>S$" + _renewalquote.ToString() + "</td></tr>";
            //_html += "</table>";

            _html += "<table style='width: 100%; background-color: white;' cellpadding='0' cellspacing='0' border='1'>";

            _html += "<tr><td colspan='4' align='left'><b>Any Claims in the Last 3 Years? : </b>" + _claimstatus.ToString() + "</td></tr>";
            if (_claimstatus == "Yes")
            {
                if (claimamt1.ToString() != "0" && claimamt1.ToString() != "")
                {
                    _html += "<tr><td style='width:25%' align='left'>Claim Date</td>"
                    + "<td style='width:25%; align='left'> " + _claim1ststat.ToString() + " to " + _claim1stend.ToString() + "</td>"
                    //+ "<td style='width:25%; align='left'> " + _dtclaim1ststat.ToString("dd-MMM-yyyy") + " to " + _dtclaim1stend.ToString("dd-MMM-yyyy") + "</td>"
                    + "<td style='width: 25%' align='left'> Claim Amount</td>"
                    + "<td style='width: 25%' align='left'>" + claimamt1.ToString() + "</td></tr>";
                }
                if (claimamt2.ToString() != "0" && claimamt2.ToString() != "")
                {
                    _html += "<tr><td style='width:25%' align='left'>Claim Date</td>"
                    + "<td style='width: 25%;' align='left'>" + _claim2ndstat.ToString() + " to " + _claim2ndend.ToString() + "</td>"
                    //+ "<td style='width: 25%;' align='left'>" + _dtclaim2ndstat.ToString("dd-MMM-yyyy") + " to " + _dtclaim2ndend.ToString("dd-MMM-yyyy") + "</td>"
                    + "<td style='width: 25%' align='left'> Claim Amount</td>"
                    + "<td style='width: 25%' align='left'>" + claimamt2.ToString() + "</td></tr>";
                }
                if (claimamt3.ToString() != "0" && claimamt3.ToString() != "")
                {
                    _html += "<tr><td style='width:25%' align='left'>Claim Date</td>"
                    + "<td style='width: 25%;' align='left'>" + _claim3rdstat.ToString() + " to " + _claim3rdend.ToString() + "</td>"
                    //+ "<td style='width: 25%;' align='left'>" + _dtclaim3rdstat.ToString("dd-MMM-yyyy") + " to " + _dtclaim3rdend.ToString("dd-MMM-yyyy") + "</td>"
                    + "<td style='width: 25%' align='left'> Claim Amount</td>"
                    + "<td style='width: 25%' align='left'>" + claimamt3.ToString() + "</td></tr>";
                }
            }

            _html += "</table>";

            _html += "<table style='width: 100%; background-color: white;' cellpadding='0' cellspacing='0' border='1'>";
            _html += "<tr><td colspan='4' align='left'><b>Additional drivers : </b>" + _additionaldriver.ToString() + "</td></tr>";
            if (_additionaldriver == "Yes")
            {
                if (Convert.ToInt16(_drcount) > 0)
                {

                    _html += "<tr><td colspan='4' align='left'>Driver 1</td></tr>"
                    + "<tr><td style='width:25%' align='left'>Name</td>"
                    + "<td style='width: 25%' align='left'>" + _driver1name + "</td>"
                    + "<td style='width: 25%' align='left'>Relation </td>"
                    + "<td style='width: 25%' align='left'> " + _driver1relation + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>NRIC</td>"
                    + "<td style='width: 25%' align='left'>" + _driver1nric + "</td>"
                    + "<td style='width: 25%' align='left'>License Pass Date </td>"
                    + "<td style='width: 25%;' align='left'> " + _driver1licensepassdate.ToString() + "</td></tr>"
                    //+ "<td style='width: 25%;' align='left'> " + _dtdriver1licensepassdate.ToString("dd-MMM-yyyy") + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>Driver DOB</td>"
                    + "<td style='width: 25%' align='left'>" + _drDOB1.ToString() + "</td>"
                    + "<td style='width: 25%' align='left'>Driving Experience</td>"
                    + "<td style='width: 25%' align='left'>" + _drexp1.ToString() + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>Gender</td>"
                    + "<td style='width: 25%' align='left'>" + _drgender1.ToString() + "</td>"
                    + "<td style='width: 25%' align='left'>Marital Status</td>"
                    + "<td style='width: 25%' align='left'>" + _drmarital1.ToString() + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>Occupation</td>"
                    + "<td style='width: 25%' align='left'>" + _drocc1.ToString() + "</td>"
                    + "<td style='width: 25%' align='left'>Job Nature</td>"
                    + "<td style='width: 25%' align='left'>" + _drjobnature1.ToString() + "</td></tr>";


                }
                if (Convert.ToInt16(_drcount) > 1)
                {
                    _html += "<tr><td colspan='4' align='left'>Driver 2</td></tr>"
                    + "<tr><td style='width:25%' align='left'>Name</td>"
                    + "<td style='width: 25%' align='left'>" + _driver2name + "</td>"
                    + "<td style='width: 25%' align='left'>Relation </td>"
                    + "<td style='width: 25%' align='left'> " + _driver2relation + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>NRIC</td>"
                    + "<td style='width: 25%' align='left'>" + _driver2nric + "</td>"
                    + "<td style='width: 25%' align='left'>License Pass Date </td>"
                    + "<td style='width: 25%;' align='left'> " + _driver2licensepassdate.ToString() + "</td></tr>"
                    //+ "<td style='width: 25%;' align='left'> " + _dtdriver2licensepassdate.ToString("dd-MMM-yyyy") + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>Driver DOB</td>"
                    + "<td style='width: 25%' align='left'>" + _drDOB2.ToString() + "</td>"
                    + "<td style='width: 25%' align='left'>Driving Experience</td>"
                    + "<td style='width: 25%' align='left'>" + _drexp2.ToString() + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>Gender</td>"
                    + "<td style='width: 25%' align='left'>" + _drgender2.ToString() + "</td>"
                    + "<td style='width: 25%' align='left'>Marital Status</td>"
                    + "<td style='width: 25%' align='left'>" + _drmarital2.ToString() + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>Occupation</td>"
                    + "<td style='width: 25%' align='left'>" + _drocc2.ToString() + "</td>"
                    + "<td style='width: 25%' align='left'>Job Nature</td>"
                    + "<td style='width: 25%' align='left'>" + _drjobnature2.ToString() + "</td></tr>";
                }
                if (Convert.ToInt16(_drcount) > 2)
                {
                    _html += "<tr><td colspan='4' align='left'>Driver 3</td></tr>"
                    + "<tr><td style='width:25%' align='left'>Name</td>"
                    + "<td style='width: 25%' align='left'>" + _driver3name + "</td>"
                    + "<td style='width: 25%' align='left'>Relation </td>"
                    + "<td style='width: 25%' align='left'> " + _driver3relation + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>NRIC</td>"
                    + "<td style='width: 25%' align='left'>" + _driver3nric + "</td>"
                    + "<td style='width: 25%' align='left'>License Pass Date </td>"
                    + "<td style='width: 25%;' align='left'> " + _driver3licensepassdate.ToString() + "</td></tr>"
                    //+ "<td style='width: 25%;' align='left'> " + _dtdriver3licensepassdate.ToString("dd-MMM-yyyy") + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>Driver DOB</td>"
                    + "<td style='width: 25%' align='left'>" + _drDOB3.ToString() + "</td>"
                    + "<td style='width: 25%' align='left'>Driving Experience</td>"
                    + "<td style='width: 25%' align='left'>" + _drexp3.ToString() + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>Gender</td>"
                    + "<td style='width: 25%' align='left'>" + _drgender3.ToString() + "</td>"
                    + "<td style='width: 25%' align='left'>Marital Status</td>"
                    + "<td style='width: 25%' align='left'>" + _drmarital3.ToString() + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>Occupation</td>"
                    + "<td style='width: 25%' align='left'>" + _drocc3.ToString() + "</td>"
                    + "<td style='width: 25%' align='left'>Job Nature</td>"
                    + "<td style='width: 25%' align='left'>" + _drjobnature3.ToString() + "</td></tr>";
                }
                if (Convert.ToInt16(_drcount) > 3)
                {
                    _html += "<tr><td colspan='4' align='left'>Driver 4</td></tr>"
                    + "<tr><td style='width:25%' align='left'>Name</td>"
                    + "<td style='width: 25%' align='left'>" + _driver4name + "</td>"
                    + "<td style='width: 25%' align='left'>Relation </td>"
                    + "<td style='width: 25%' align='left'> " + _driver4relation + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>NRIC</td>"
                    + "<td style='width: 25%' align='left'>" + _driver4nric + "</td>"
                    + "<td style='width: 25%' align='left'>License Pass Date </td>"
                    + "<td style='width: 25%;' align='left'> " + _driver4licensepassdate.ToString() + "</td></tr>"
                    //+ "<td style='width: 25%;' align='left'> " + _dtdriver4licensepassdate.ToString("dd-MMM-yyyy") + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>Driver DOB</td>"
                    + "<td style='width: 25%' align='left'>" + _drDOB4 + "</td>"
                    + "<td style='width: 25%' align='left'>Driving Experience</td>"
                    + "<td style='width: 25%' align='left'>" + _drexp4.ToString() + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>Gender</td>"
                    + "<td style='width: 25%' align='left'>" + _drgender4.ToString() + "</td>"
                    + "<td style='width: 25%' align='left'>Marital Status</td>"
                    + "<td style='width: 25%' align='left'>" + _drmarital4.ToString() + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>Occupation</td>"
                    + "<td style='width: 25%' align='left'>" + _drocc4.ToString() + "</td>"
                    + "<td style='width: 25%' align='left'>Job Nature</td>"
                    + "<td style='width: 25%' align='left'>" + _drjobnature4.ToString() + "</td></tr>";
                }
                if (Convert.ToInt16(_drcount) > 4)
                {
                    _html += "<tr><td colspan='4' align='left'>Driver 5</td></tr>"
                    + "<tr><td style='width:25%' align='left'>Name</td>"
                    + "<td style='width: 25%' align='left'>" + _driver5name + "</td>"
                    + "<td style='width: 25%' align='left'>Relation </td>"
                    + "<td style='width: 25%' align='left'> " + _driver5relation + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>NRIC</td>"
                    + "<td style='width: 25%' align='left'>" + _driver5nric + "</td>"
                    + "<td style='width: 25%' align='left'>License Pass Date </td>"
                    + "<td style='width: 25%;' align='left'> " + _driver5licensepassdate.ToString() + "</td></tr>"
                    //+ "<td style='width: 25%;' align='left'> " + _dtdriver5licensepassdate.ToString("dd-MMM-yyyy") + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>Driver DOB</td>"
                    + "<td style='width: 25%' align='left'>" + _drDOB5 + "</td>"
                    + "<td style='width: 25%' align='left'>Driving Experience</td>"
                    + "<td style='width: 25%' align='left'>" + _drexp5.ToString() + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>Gender</td>"
                    + "<td style='width: 25%' align='left'>" + _drgender5.ToString() + "</td>"
                    + "<td style='width: 25%' align='left'>Marital Status</td>"
                    + "<td style='width: 25%' align='left'>" + _drmarital5.ToString() + "</td></tr>"

                    + "<tr><td style='width:25%' align='left'>Occupation</td>"
                    + "<td style='width: 25%' align='left'>" + _drocc5.ToString() + "</td>"
                    + "<td style='width: 25%' align='left'>Job Nature</td>"
                    + "<td style='width: 25%' align='left'>" + _drjobnature5.ToString() + "</td></tr>";
                }
            }
            _html += "</table>";
            #endregion

            lblPremium.Text = _html;
            _fileName = CECarId.ToString();
            ExporttoExcel(_fileName, lblPremium, strText.ToString());

        }

    }

    public void ExporttoExcel(string fileName, Label lblPremium, string headerText)
    {
        string attachment = "attachment; filename=" + fileName + ".xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        // Create a form to contain the grid
        HtmlForm frm = new HtmlForm();
        frm.InnerHtml = headerText;
        lblPremium.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(lblPremium);
        frm.RenderControl(htw);
        //GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();

        //string attachment = "attachment; filename=" + fileName + ".htm";
        //Response.ClearContent();
        //Response.AddHeader("content-disposition", attachment);
        //Response.ContentType = "text/HTML";
        //StringWriter sw = new StringWriter();
        //HtmlTextWriter htw = new HtmlTextWriter(sw);
        //HtmlForm frm = new HtmlForm();
        //frm.InnerHtml = headerText;
        //lblPremium.Parent.Controls.Add(frm);
        //frm.Attributes["runat"] = "server";
        //frm.Controls.Add(lblPremium);
        //frm.RenderControl(htw);
        //Response.Write(sw.ToString());
        //Response.End();

    }

}
